package com.jic.interaction.ChinaPost.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.common.enums.RedisKey;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * lql
 */
@EnableScheduling
@Component
@Slf4j
public class WzCashAccessTokenTask {

    @Autowired
    private RedisService redisService;

    @Value("${wz.wzCash.appid}")
    private String appid;

    @Value("${wz.wzCash.secret}")
    private String secret;

    /**
     * 地址需要经过本地nginx转发
     */
    @Value("${wz.wzCash.url}")
    private String url;
    /**
     * 更新AccessToken
     */
    @Scheduled(fixedDelay = 50 * 60 * 1000)
    public void updateAccessTokenTask() {
        log.info("更新微众收银台token定时任务开始...");
        try {
            Map<String, String> params = new HashMap<>();
            params.put("app_id",appid);
            params.put("secret",secret);
            params.put("grant_type","client_credential");
            params.put("version","1.0.0");
            log.info("请求地址参数{}",params);
            String body = HttpClientUtil.doRequestGet(this.url+"/m1/api/oauth2/access_token",params,null);
            if (StringUtils.isNotEmpty(body)) {
                JSONObject jsonObject= JSON.parseObject(body);
                String code = jsonObject.getString("code");
                // code不是0是表示获取失败
                if("0".equals(code)){
                    String wz_pay_token = jsonObject.getString("access_token");
                    // 将ACCESS_TOKEN存入缓存
                    redisService.setValue(RedisKey.WZ_CASH_TOKEN,wz_pay_token,60000L);
                    // 更新SingTicket
                    updateSingTicket(wz_pay_token);
                } else {
                    log.error(jsonObject.getString("msg"));
                }
            } else {
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("====同步token结束====");
    }


    /**
     * 更新票据
     */
    private void updateSingTicket(String wz_pay_token){
        try {
            Map<String, String> params = new HashMap<>(); //创建map
            params.put("app_id",appid); //添加appid
            params.put("access_token",wz_pay_token); //添加token
            params.put("type","SIGN");
            params.put("version","1.0.0");
            String body = HttpClientUtil.doRequestGet(this.url+"m1/api/oauth2/api_ticket",params,null); //请求
            if (StringUtils.isNotEmpty(body)) {
                JSONObject jsonObject= JSON.parseObject(body);
                String code = jsonObject.getString("code");
                // code不是0是表示获取失败
                if("0".equals(code)){
                    JSONArray tickets = jsonObject.getJSONArray("tickets");
                    JSONObject ticket = tickets.getJSONObject(0);
                    String singTicket = ticket.getString("value");
                    // 将SingTicket存入缓存
                    redisService.setValue(RedisKey.WZ_CASH_TICKET,singTicket,6000L);
                } else {
                    log.error(jsonObject.getString("msg"));
                }
            } else {
                log.error("更新SingTicket请求失败");
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

